//
//  UIView+cyFrame.m
//  cycleControlExtension
//
//  Created by ddy on 2018/8/8.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "UIView+cyFrame.h"

@implementation UIView (cyFrame)
///x
-(void)setCy_x:(CGFloat)cy_x{
    CGRect frame = self.frame;
    frame.origin.x = cy_x;
    self.frame = frame;
}
-(CGFloat)cy_x{
    return self.frame.origin.x;
}
///y
-(void)setCy_y:(CGFloat)cy_y{
    CGRect frame = self.frame;
    frame.origin.y = cy_y;
    self.frame = frame;
}

- (CGFloat)cy_y
{
    return self.frame.origin.y;
}
//centerX
-(void)setCy_centerX:(CGFloat)cy_centerX{
    CGPoint center = self.center;
    center.x = cy_centerX;
    self.center = center;
}
-(CGFloat)cy_centerX{
    return self.center.x;
}
///centerY
- (void)setCy_centerY:(CGFloat)cy_centerY{
    CGPoint center = self.center;
    center.y = cy_centerY;
    self.center = center;
}
-(CGFloat)cy_centerY{
    return self.center.y;
}
//width
-(void)setCy_width:(CGFloat)cy_width{
    CGRect frame = self.frame;
    frame.size.width = cy_width;
    self.frame = frame;
}
- (CGFloat)cy_width
{
    return self.frame.size.width;
}

//height
-(void)setCy_height:(CGFloat)cy_height{
    CGRect frame = self.frame;
    frame.size.height = cy_height;
    self.frame = frame;
}
-(CGFloat)cy_height{
    return self.frame.size.height;
}


///size
-(void)setCy_size:(CGSize)cy_size{
    CGRect frame = self.frame;
    frame.size = cy_size;
    self.frame = frame;
}
- (CGSize)cy_size
{
    return self.frame.size;
}
//right
-(void)setCy_right:(CGFloat)cy_right{
    CGRect frame = self.frame;
    frame.origin.x = cy_right - frame.size.width;
    self.frame = frame;
}
- (CGFloat)cy_right
{
    return self.frame.origin.x + self.frame.size.width;
}

///bottom
-(void)setCy_bottom:(CGFloat)cy_bottom{
    CGRect frame = self.frame;
    frame.origin.y = cy_bottom - frame.size.height;
    self.frame = frame;
}
- (CGFloat)cy_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

//origin
-(void)setCy_origin:(CGPoint)cy_origin{
    CGRect frame = self.frame;
    frame.origin = cy_origin;
    self.frame = frame;
}
- (CGPoint)cy_origin
{
    return self.frame.origin;
}
/**
 * 判断一个控件是否真正显示在主窗口
 */
- (BOOL)cy_isShowingOnKeyWindow{
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect windowBounds = [keyWindow bounds];
    BOOL isShowing = CGRectIntersectsRect(newFrame, windowBounds);
    
    //  (1)!self.isHidden : 该控制器没有被隐藏
    //  (2)self.alpha : 该控制器可见
    //  (3)self.window ：该控制器对应的window等于keyWindow
    //  (4) isShowing :  (newFrame:“由”某块区域的self.frame，原属于self.superview,“转化”到keywindow上的坐标)
    
    return !self.isHidden&&self.alpha>0.01&&self.window==keyWindow&&isShowing;
    
}
@end
